﻿using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using DingTalkAPIHelp.Base;
using System.Text.RegularExpressions;
using static DingTalk.Api.Request.OapiAttendanceShiftAddRequest;
using static DingTalk.Api.Response.OapiAttendanceGetsimplegroupsResponse;

namespace DingTalkAPIHelp.Kaoqin
{
    public class KaoqinManager: SysConfigManager
    {
        public OapiAttendanceGetupdatedataResponse getUserData(string userid,String date)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getupdatedata");
            OapiAttendanceGetupdatedataRequest req = new OapiAttendanceGetupdatedataRequest();
            req.Userid = userid;
            req.WorkDate = DateTime.Parse(date);
            OapiAttendanceGetupdatedataResponse rtdata = client.Execute(req,accessToken);
            return rtdata;
        }
        #region 班次管理
        /// <summary>
        /// 获取班次列表
        /// </summary>
        /// <returns></returns>
        public OapiAttendanceShiftListResponse GetShiftList()
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/shift/list");
            OapiAttendanceShiftListRequest req = new OapiAttendanceShiftListRequest();
            req.OpUserId = "manager4990";
            OapiAttendanceShiftListResponse rsp = client.Execute(req, appAccessToken);
            return rsp;
        }

        /// <summary>
        /// 添加/修改 班次
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public OapiAttendanceShiftAddResponse SaveShift(TopAtClassVoDomain info)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/shift/add");
            OapiAttendanceShiftAddRequest req = new OapiAttendanceShiftAddRequest();
            req.OpUserId = "manager4990";
            req.Shift_ = info;
            OapiAttendanceShiftAddResponse rsp = client.Execute(req, appAccessToken);
            return rsp;
        }

        /// <summary>
        /// 删除班次
        /// </summary>
        /// <param name="shiftId"></param>
        /// <returns></returns>
        public OapiAttendanceShiftDeleteResponse DelShift(long shiftId)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/shift/delete");
            OapiAttendanceShiftDeleteRequest req = new OapiAttendanceShiftDeleteRequest();
            req.OpUserId = "manager4990";
            req.ShiftId = shiftId;

            OapiAttendanceShiftDeleteResponse rsp = client.Execute(req, appAccessToken);
            return rsp;
        }

        /// <summary>
        /// 获取班次详情
        /// </summary>
        /// <param name="lid"></param>
        /// <returns></returns>
        public OapiAttendanceShiftQueryResponse GetShiftDetail(long lid)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/shift/query");
            OapiAttendanceShiftQueryRequest req = new OapiAttendanceShiftQueryRequest();
            req.OpUserId = "manager4990";
            req.ShiftId = lid;
            OapiAttendanceShiftQueryResponse rsp = client.Execute(req, appAccessToken);
            return rsp;
        }

        #endregion

        #region 考勤组管理
        public OapiAttendanceGroupMinimalismListResponse GetGroupList()
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/group/minimalism/list");
            OapiAttendanceGroupMinimalismListRequest req = new OapiAttendanceGroupMinimalismListRequest();
            req.OpUserId = "manager4990";

            OapiAttendanceGroupMinimalismListResponse rsp = client.Execute(req, appAccessToken);
            return rsp;
        }

        public OapiAttendanceGroupQueryResponse GetGroupDetail(long groupId)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/group/query");
            OapiAttendanceGroupQueryRequest req = new OapiAttendanceGroupQueryRequest();
            req.GroupId= groupId;
            req.OpUserId = "manager4990";
            OapiAttendanceGroupQueryResponse rsp = client.Execute(req, appAccessToken);
            return rsp;
        }

        public OapiAttendanceGetsimplegroupsResponse GetGroupDetailList(int page,int PageSize)
        {
            int startOffSet = (page-1)*PageSize;
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getsimplegroups");
            OapiAttendanceGetsimplegroupsRequest req = new OapiAttendanceGetsimplegroupsRequest();
            req.Offset = startOffSet;
            req.Size = PageSize;
            OapiAttendanceGetsimplegroupsResponse rsp = client.Execute(req, appAccessToken);
            if (rsp.Result.HasMore)
            {
                GetGroupNextPage(rsp, (startOffSet + 10));
            }
            return rsp;
        }

        private void GetGroupNextPage(OapiAttendanceGetsimplegroupsResponse root, int offset)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getsimplegroups");
            OapiAttendanceGetsimplegroupsRequest req = new OapiAttendanceGetsimplegroupsRequest();
            req.Offset = offset;
            req.Size = 10;
            OapiAttendanceGetsimplegroupsResponse rsp = client.Execute(req, appAccessToken);
            root.Result.Groups.AddRange(rsp.Result.Groups);
            if (rsp.Result.HasMore)
            {
                GetGroupNextPage(root, (offset + 10));
            }
        }
        #endregion

    }
}
